using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class InventoryItem : LoadableJSONObject{
	public float weight;
	
	public float totalWeight
	{
		get{return weight*quantity;}
	}
	
	public static List<string> validSlots = new List<string>{
		"None", "Ammunition", "Armor", "Belt", "Body", "Chest", "Eyes", "Feet", "Hands", 
		"Head", "Headband", "Neck", "Ring", "Shield", "Shoulders", "Weapon", "Wrist"
	};
	
	public List<BuffDebuff> buffs; 
	public string slot = "None";
	
	public bool HasCharges = false;
	public int chargesRemaining = 50;
	
	public int quantity = 1;
	public float worth = 0;
	protected bool equipped = false;
	
	public bool isEquipped
	{
		get
		{
			return equipped;
		}
	}
	
	public void EquipToggle()
	{
		SheetManager.activeCharacter.RefreshBuffsDatabase(buffs);
		equipped = !equipped;
		SheetManager.activeCharacter._storedBuffs = null;
	}
	
	
	private string _protectiveItemDescription;
	public string protectiveItemDescription
	{
		get
		{
			if(string.IsNullOrEmpty(_protectiveItemDescription))
			{
				string desc = "";
				
				Dictionary<string,int> reference = new Dictionary<string, int>();
				buffs.ForEach((b) =>
					{
						b.modifiers.FindAll(m => m.toStat == "AC").ForEach( m =>
							{
								if(reference.ContainsKey(m.type))
								{
									if(reference[m.type] < m.amount)
										reference[m.type] = m.amount;
								}
								else
									reference.Add(m.type, m.amount);
							}
						);
					}	
				);
				int amount = 0;
				foreach(int a in reference.Values)
				{
					amount += a;
				}
				
				desc += "+" + amount + " AC";
				
				amount = 999;
				buffs.ForEach((b) =>
					{
						b.modifiers.FindAll(m => m.toStat == "Max. Dex Bonus").ForEach( m =>
							{
								if(amount > m.amount)
									amount = m.amount;
							}
						);
					}	
				);
				if(amount < 999)
					desc += ", " + amount + " Max Dex";
				
				amount = 0;
				buffs.ForEach((b) =>
					{
						b.modifiers.FindAll(m => m.toStat == "Armor Check").ForEach( m =>
							{
								if(amount < m.amount)
									amount = m.amount;
							}
						);
					}	
				);
				if(amount > 0)
					desc += ", -" + amount + " ACP";
				
				_protectiveItemDescription = desc;
			}
			
			return _protectiveItemDescription;
		}
	}
	
	public InventoryItem()
	{
	}
	
	public override void Load(string json, string file)
	{
		fileName = file;
		if(json != null)
		{
			Dictionary<string,string> data = (MiniJSON.Json.Deserialize(json) as Dictionary<string,object>).ToDSS();
			
			name = data["name"];
			description = data["description"];
			weight = float.Parse(data["weight"]);
			quantity = int.Parse(data["quantity"]);
			chargesRemaining = int.Parse(data["chargesRemaining"]);
			HasCharges = bool.Parse(data["HasCharges"]);
			
			buffs = new List<BuffDebuff>();
			List<object> objectsJson = MiniJSON.Json.Deserialize(data["buffs"]) as List<object>;
			foreach(string objJson in objectsJson)
			{
				BuffDebuff o = new BuffDebuff();
				o.Load(objJson, "");
				buffs.Add(o);
			}
			
			slot = data["slot"];
			worth = float.Parse(data["worth"]);
			
			equipped = bool.Parse(data["equipped"]);
		}
	}
	
	public override string ToJSON()
	{
		Dictionary<string,string> data = new Dictionary<string, string>();
		data.Add("name", name);
		data.Add("description", description);
		data.Add("weight", weight.ToString());
		data.Add("buffs", buffs.ToJSON());
		data.Add("slot",slot);
		data.Add("HasCharges", HasCharges.ToString());
		data.Add("chargesRemaining", chargesRemaining.ToString());
		data.Add("quantity", quantity.ToString());
		data.Add("worth", worth.ToString());
		data.Add("equipped", equipped.ToString());
		
		return MiniJSON.Json.Serialize(data);
	}
	
	public virtual InventoryItem DeepCopy()
	{
		InventoryItem a = new InventoryItem();
		a.Load(ToJSON(), fileName);
		return a;
	}
}
